/*
 * This file is part of jVisualFS.
 * Version: release-0.8_beta
 *
 * jVisualFS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jVisualFS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jVisualFS.  If not, see <http://www.gnu.org/licenses/>.
 */

package jvisualfs;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

/**
 *
 * @author Dag Rune Sneeggen
 */
public class GenFS {
    public static void traverse(DirStruct dir, DirStruct parent, DirCollection col, boolean isRoot) {
        System.out.println("Starting traverse..." + dir.getName() + " PARENT: " + parent.getName());
        if (isRoot) {
            col.get(dir.getName()).setParent(parent);
        } else {
            col.get(dir.getName()).setParent(parent);
        }        
        FileFilter BadNodesFilter = new FileFilter() {
            public boolean accept(File file) {
                /*if (file.canRead()) {
                    return true;
                } else {
                    return false;
                }*/
                return file.canRead();
                /*try {
                    //System.out.println("OK");
                     return file.getCanonicalPath().equals(file.getCanonicalPath());
                } catch (IOException e) {
                    //System.out.println("NOK");
                    return false;
                }*/
            }
        };
        //System.out.println("SLF: " + SymlinksFilter.toString());
        File[] list = dir.getFp().listFiles(BadNodesFilter);
        //System.out.println("LISTLEN: " + list.toString());
        for (File tmpdir : list) {
            System.out.println("TNAME: " + tmpdir.getAbsolutePath() /*+ tmpdir.isDirectory() + tmpdir.isFile()*/);
            if (tmpdir.isDirectory()) {
                //System.out.println("DNAME: " + tmpdir.getAbsolutePath() + " DIR!!!");
                if (!tmpdir.getName().contains("System Volume Information"))  {
                    traverse(col.add(new DirStruct(tmpdir)), dir, col, false); 
                }
                
                //long newlength = col.get(dir.getName()).getLength() + 5;
                //col.get(dir.getName()).setLength(newlength);
            } else if (tmpdir.isFile()) {
                //System.out.println("FNAME: " + tmpdir.getAbsolutePath() + " FILE: " + tmpdir.length());
                float newlength = col.get(dir.getName()).getLength() + (float)tmpdir.length();
                col.get(dir.getName()).setLength(newlength);
            }            
            //long newlength = col.get(dir.getName()).getLength() + tmpdir.length();
            //col.get(dir.getName()).setLength(newlength);
        }
        System.out.println("Done traversing!");
        if (!isRoot) {
            //System.out.println(col.get(dir.getParent().getName()).getLength() + " -_- " + col.get(dir.getName()).getLength());
            float travlength = col.get(dir.getParent().getName()).getLength() + col.get(dir.getName()).getLength();        
            //System.out.println("TRAVL: " + travlength);
            col.get(dir.getParent().getName()).setLength(travlength);
            //System.out.println("DLEN: " + col.get(dir.getParent().getName()).getLength());
        }
    }
}
